<template>
  <div>
    <el-card>
      <el-row type="flex" justify="end">
        <el-button
          type="primary"
          size="small"
          @click="addPerm('0','1')"
        >添加权限</el-button>
      </el-row>
      <el-divider />
      <!-- 表格 -->
      <el-table
        :data="tableData"
        row-key="id"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          label="权限名称"
          width="300"
        />
        <el-table-column
          prop="code"
          label="权限标识"
          width="300"
        />
        <el-table-column
          prop="description"
          label="权限描述"
        />
        <el-table-column
          prop="address"
          label="操作"
          width="300"
        >
          <template v-slot="{row}">
            <el-row type="flex" justify="center">
              <el-button v-if="row.type==='1'" type="primary" size="small" @click="addPerm(String(row.id),'2')">添加子权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出组件 -->
    <AddQuanXian
      ref="addQuanXian"
      :show-dialog.sync="showDialog"
      :alltable-datalist="alltableDatalist"
      :table-data="tableData"
      @updateEvent="getQuanxian"
    />
  </div>
</template>

<script>
import AddQuanXian from './components/add-quanxian.vue'
import { getQuanXianList, delteQuanXian } from '@/api/quanxian'
export default {
  components: { AddQuanXian },
  data() {
    return {
      alltableDatalist: [], // 全部数据
      tableData: [], // 权限列表
      limit: 1000, // 每页条数
      start: 0, // 开始获取的位置
      showDialog: false // 控制弹窗的显示
    }
  },
  async created() {
    this.getQuanxian()
  },
  methods: {
    // 点击编辑按钮
    async editFn(id) {
      this.showDialog = true
      this.$refs.addQuanXian.getQuanxian(id)
    },
    // 点击删除按钮
    delFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delteQuanXian(id)
        this.getQuanxian()
      }).catch(() => {

      })
    },
    // 点击添加权限
    async addPerm(pid, type) {
      this.showDialog = true
      this.$refs.addQuanXian.formData.pid = pid
      this.$refs.addQuanXian.formData.type = type
    },
    // 平铺数据转树形
    listToTree(list, id) {
      const arr = []
      list.forEach(item => {
        if (item.pid === id) { // 选出父级
          arr.push(item)
          item.children = [] // 表示给item对象添加children属性，并赋值为空数组
          list.forEach(val => {
            if (item.id === Number(val.pid)) {
              item.children.push(val)
            }
          })
        }
      })
      return arr
    },
    // 获取最新权限数据
    async getQuanxian() {
      const res = await getQuanXianList({
        _limit: this.limit,
        _start: this.start
      })
      this.alltableDatalist = res.data
      this.tableData = this.listToTree(res.data, '0')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 15px;
}
</style>
